package com.zhongjian.technologycal.mall.activity.main.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.main.activity.CourseDetailsActivity;
import com.zhongjian.technologycal.mall.activity.main.bean.BreifBean;
import com.zhongjian.technologycal.mall.activity.web.WebViewMarkActivity;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.base.BaseFragment;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.LogUtils;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.ToastUitl;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on
 * 课程简介
 */

public class CourseDetailsbreifFragment extends BaseFragment {
    public static final String CATEGORY_NAME = "com.nanchen.aiyagirl.module.category.CategoryFragment.CATEGORY_NAME";
    @BindView(R.id.fragment_details_breif_webview)
    WebView fragment_details_breif_webview;
    @BindView(R.id.fragment_details_breif_time)
    TextView fragment_details_breif_time;
    @BindView(R.id.fragment_details_breif_address)
    TextView fragment_details_breif_address;
    @BindView(R.id.fragment_details_breif_desc)
    TextView fragment_details_breif_desc;
    private String courseId;
    private String descUrl;

    @Override
    protected void initView() {
        initViewRequest();
        initViewWebView();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_course_details_breif;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        courseId = ((CourseDetailsActivity) activity).getCourseId();
    }

    public static CourseDetailsbreifFragment newInstance(String mCategoryName) {
        CourseDetailsbreifFragment categoryFragment = new CourseDetailsbreifFragment();
        Bundle bundle = new Bundle();
        bundle.putString(CATEGORY_NAME, mCategoryName);
        categoryFragment.setArguments(bundle);
        return categoryFragment;
    }

    @Override
    public void initPresenter() {

    }

    @OnClick(R.id.fragment_details_breif_desc)
    public void initOnClickDesc(){
        Intent intent = new Intent();
        intent.setClass(getActivity(), WebViewMarkActivity.class);
        intent.putExtra("url", BaseContacts.BASE_URL + descUrl);
        startActivity(intent);

    }

    private void initViewRequest() {
        if (!NetWorkUtils.isNetConnected(getActivity())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getActivity())
                .getUrlRequestCoursedetailbrief(Integer.valueOf(courseId),
                        new Subscriber<ResponseBody>() {
                            @Override
                            public void onStart() {
                                super.onStart();
                            }

                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
                                try {
                                    Gson gson = new Gson();
                                    BreifBean breifBean = gson.fromJson(responseBody.string(), BreifBean.class);
                                    if (breifBean.getError_code() == 0) {
                                        fragment_details_breif_time.setText("开课时间： "+ breifBean.getData().getCourse().getCoursestarttime());
                                        fragment_details_breif_address.setText("开课地点： " + breifBean.getData().getCourse().getCoursesite());
                                        fragment_details_breif_desc.setText(""+
                                                breifBean.getData().getCoursedatalist().get(0).getTitle());
                                        descUrl = breifBean.getData().getCoursedatalist().get(0).getSrc();
                                    } else {
                                        ToastUitl.showShort(breifBean.getError_message());
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        });


    }

    private void initViewWebView() {
        fragment_details_breif_webview.loadUrl(BaseContacts.URL_COURSE_ICON_DETAILS + courseId);
        WebSettings settings = fragment_details_breif_webview.getSettings();
        settings.setLoadWithOverviewMode(true);
        settings.setJavaScriptEnabled(true);
        settings.setAppCacheEnabled(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setSupportZoom(true);

        fragment_details_breif_webview.setWebChromeClient(new MyWebChrome());
        fragment_details_breif_webview.setWebViewClient(new MyWebClient());
    }

    private class MyWebChrome extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);

        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
        }
    }


    private class MyWebClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            LogUtils.logd("截取URL", url);
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

        }
    }


}
